home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 6462 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.6 KB  |  80 lines

  1. Path: admaix.sunydutchess.edu!ub!newserve!rebecca!rpi!not-for-mail
  2. From: rcauvin@natinst.com (Roger L. Cauvin)
  3. Newsgroups: comp.lang.c++.moderated,comp.lang.c++
  4. Subject: Re: Virtual Function in private part?
  5. Date: 8 Feb 1996 16:33:17 -0000
  6. Organization: National Instruments
  7. Sender: cppmods@netlab.cs.rpi.edu
  8. Approved: Dietmar.Kuehl@uni-konstanz.de
  9. Message-ID: <4fd8kd$9i7@netlab.cs.rpi.edu>
  10. References: <4fa17f$qk4@netlab.cs.rpi.edu>
  11. NNTP-Posting-Host: netlab.cs.rpi.edu
  12. X-Original-Date: 8 Feb 1996 15:36:33 GMT
  13.  
  14. In article <4fa17f$qk4@netlab.cs.rpi.edu>, Paul.Henshaw@jrc.it wrote:
  15.  
  16. > Though Roger's argument is (IMHO) valid in general, I have a specific example
  17. > where I feel I am justified in keeping virtual methods private:
  18. > class ActiveObject {
  19. > public:
  20. >         ActiveObject(void* data=NULL, options_t=Normal);
  21. >         virtual ~ActiveObject();
  22.           ...
  23. > private:
  24. >         virtual void* main(void* data)=0;
  25. > }
  26. > class ActiveSocketReader : public ActiveObject {
  27. > public:
  28.          ...
  29. >         }
  30. > private:
  31. >         virtual void* main(void* data);
  32. >         
  33. > }
  34.  
  35. [portion of example elided]
  36.  
  37. > Since main should be called only by ActiveObject::ActivObject() main should
  38. > be private - however the derived class must be able to provide its own main
  39. > function in order to be useful.
  40.  
  41. It is not necessarily the case that main should be called only by the
  42. ActiveObject base class.  Consider a SpecialActiveSocketReader class
  43. derived from ActiveSocketReader.  An instance of the
  44. SpecialActiveSocketReader is just like an instance of the
  45. ActiveSocketReader class, except that it performs some additional
  46. processing in the main member function.  The first thing that the
  47. SpecialActiveSocketReader::main function should do is to call the
  48. ActiveSocketReader::main function.  It cannot do so, however, because it
  49. was declared private instead of protected.
  50.  
  51. class SpecialActiveSocketReader : public ActiveSocketReader
  52.    {
  53.    public:
  54.       ...
  55.    private:
  56.       virtual void* main(void* data);
  57.    };
  58.  
  59. void * SpecialActiveSocketReader::main(void *data)
  60.    {
  61.    // Perform default processing.
  62.    ActiveSocketReader::main(data);    // <--- Compiler error:  main is private.
  63.  
  64.    // Perform "special" extra processing.
  65.    }
  66.  
  67. By declaring main private, you are making it impossible to derive from
  68. ActiveSocketReader in this manner.
  69.  
  70.  
  71. Roger
  72.  
  73.       [ Articles to moderate: mailto:c++-submit@netlab.cs.rpi.edu ]
  74.       [  Read the C++ FAQ: http://www.connobj.com/cpp/cppfaq.htm  ]
  75.       [  Moderation policy: http://www.connobj.com/cpp/guide.htm  ]
  76.       [      Comments? mailto:c++-request@netlab.cs.rpi.edu       ]
  77.